let express = require('express');
let router = express.Router();
let common = require('./common');


router.post('/', function(req, res){
  let {username, pwd, code} = req.body;
  console.log(username, pwd, code);
  if(code !== req.session.vimg){
    res.send({status: '-1', message: '验证码错误'});
  } else {
    common.readFile(common.userFile, function(data){
      console.log(data, username, data[username]);
      if(data[username]){
        res.send({status: '-1', message: '用户已存在'});
      }else {
        data[username] = {
          username: username,
          password: pwd
        }
        common.writeFile(common.userFile, data, function(data){
          if(data){
            res.send({status: '0'});
          } else {
            res.send({status: '-1', message: '注册失败'});
          }
        })
      }
    })
  }
})

module.exports = router;
